﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using System.Web.UI.DataVisualization.Charting;
using Entities.Enums;

namespace Interview_Prov
{
    public partial class CommonStats : System.Web.UI.Page
    {
        Business.BusinessLogic bll = new Business.BusinessLogic();
        Interview SrcInterview;
        public List<Question> Questions;

        protected void Page_Load(object sender, EventArgs e)
        {
            int interview_id = GetInterviewID();
            SrcInterview = bll.LoadInterview(GetInterviewID());
            Questions = SrcInterview.Questions;
            question_title.Visible = false;
            if (!IsPostBack)
            {
                if (interview_id == -1)
                {
                    Response.Redirect("~/SelectInterview.aspx?ReturnUrl=/CommonStats.aspx", true);
                }
                if (SrcInterview.ID == -1)
                {
                    Response.Redirect("~/Errors/InterviewLoadError.aspx", true);
                    return;
                }
                DataBind();
            }

            InterviewResultData ird = bll.GetGenericStatistics(interview_id);
            var quest = Questions[q_selecter.SelectedIndex];
            question_title.Visible = true;
            question_title.Text = quest.QuestionText;
            if (ird.ResultData.Count() == 0 || !ird.ResultData.ContainsKey(quest.ID))
            {
                Deny();
                return;
            }
            if (ird.ResultData[quest.ID].Count() == 0)
            {
                Deny();
                return;
            }

            var chart_ctl = Chart1;
            chart_ctl.Titles.Add(quest.QuestionText);

            var yValues = ird.ResultData[quest.ID].Values;
            var xValues = new List<string>();
            if (quest.Type != QuestionType.TextInput)
            {
                foreach (var answ in ird.ResultData[quest.ID].Keys)
                {
                    if (quest.Type == QuestionType.SingleChoice)
                    {
                        int parsedval = 0;
                        if (!int.TryParse(answ, out parsedval))
                        {
                            Deny();
                            return;
                        }
                        else
                        {
                            if (parsedval < 0 || parsedval > quest.Answers.Count())
                            {
                                Deny();
                                return;
                            }
                            xValues.Add(quest.Answers[parsedval]);
                        }
                    }
                    else
                    {
                        var selected_indexes = string.Join(",", answ.Split(',').Select((x) => (quest.Answers[int.Parse(x)])));
                        xValues.Add(selected_indexes);
                    }
                }
            }
            else
            {
                xValues = ird.ResultData[quest.ID].Keys.ToList();
            }

            chart_ctl.Series["default"].ChartType = SeriesChartType.Pie;
            chart_ctl.Series["default"].Points.DataBindXY(xValues, yValues);


        }

        protected void Deny()
        {
            question_title.Visible = false;
            no_results.Visible = true;
            Chart1.Visible = false;
        }

        protected int GetInterviewID()
        {
            int iid = -1;
            if (Request.Params["InterviewID"] != null)
            {
                int.TryParse(Request.Params["InterviewID"], out iid);
            }
            return iid;
        }
    }
}